<?php
/**
 *
 * @package 
 * @since 1.0
 * @todo 登陆 注册
 */
 
class ToupAction extends AllAction{

	/**
	 * ------------------------------
	 * 权限判断
	 * ------------------------------
	 */
	function pan_quan_ajax($data){  //ajax判断
		$pan=in_array($data,$_SESSION['admin_quan']);
        if($pan){
			return true;
		}else{
			return false;
		}
	}
	function pan_quan($data){ //php跳转判断
		$pan=in_array($data,$_SESSION['admin_quan']);
        if(!$pan){
			$this->assign('jumpUrl', U('User/login'));
			$this->error('请先登录');
		}
	}
	
	function toup(){

		$tid=$_GET['tid'];
		$toup=M('tou')->where("state=1")->find($_GET['tid']);
		$user=M('admin')->find($toup['uid']);
		if(!empty($user['nicheng'])){
			$toup['user_nc']=$user['nicheng'];
		}else{
			$toup['user_nc']=$user['adm_name'];
		}
		if($toup['user_nc']=="admin"){
			$toup['user_nc']="Trinity";
		}
		foreach($toup as $k=>$v){
			if(get_magic_quotes_gpc()!='-1' || get_magic_quotes_gpc()==true){
				$toup[$k]=stripslashes($v); 
			}
		}
			$toup=$this->get_lang($toup,"name");
			$toup=$this->get_lang($toup,"content");
		$this->assign('toup', $toup);
		$tou_shi=M("tou_shi")->where("tid=".$toup['id']." and state=1")->order('id asc')->select();
			$tou_shi=$this->get_lang_attr($tou_shi,"name");
			$tou_shi=$this->get_lang_attr($tou_shi,"jianjie");
			$tou_shi=$this->get_lang_attr($tou_shi,"img");
		foreach($tou_shi as $k=>$v){
			$tou_shi[$k]['img']=$this->get_thumb($v['img']);
			$tou_shi[$k]['tou_xuan_all']=M('tou_xuan')->where("tid=".$toup['id']." and sid=0 and state=1")->order('id asc')->select();
			foreach($tou_shi[$k]['tou_xuan_all'] as $kk=>$vv){
				$num1=M('tou_jieguo')->where("xid=".$vv['id']." and sid=".$v['id']." and tid=".$toup['id']." and state=1")->count();
				$num2s=M('tou_jieguo')->where("xid=".$vv['id']." and sid=".$v['id']." and tid=".$toup['id']." and state=1 and uid=1")->find();
				$num2=$num2s['jishu'];
				$tou_shi[$k]['tou_xuan_all'][$kk]['piaoshu']=$num1+$num2;
			}
			$tou_shi[$k]['tou_xuan_dan']=M('tou_xuan')->where("tid=".$toup['id']." and sid=".$v['id']." and state=1")->order('id asc')->select();
			foreach($tou_shi[$k]['tou_xuan_dan'] as $kk=>$vv){
				$num1s=M('tou_jieguo')->where("xid=".$vv['id']." and sid=".$v['id']." and tid=".$toup['id']." and state=1")->count();
				$num2ss=M('tou_jieguo')->where("xid=".$vv['id']." and sid=".$v['id']." and tid=".$toup['id']." and state=1 and uid=1")->find();
				$num2s=$num2ss['jishu'];
				$tou_shi[$k]['tou_xuan_dan'][$kk]['piaoshu']=$num1s+$num2s;
			}
		}
		$this->assign('tou_shi', $tou_shi);
		
//		if(empty($_SESSION['menuid'])){
//			$all_menu = M('pz_menu')->where("mid=0 and state=1")->order("id asc")->select();
//			$all_menu_num = M('pz_menu')->where("mid=0 and state=1")->order("id asc")->count();
//			$rand_num=rand(0,$all_menu_num-1);
//			$menuid=$all_menu[$rand_num]['id'];
//		}else{
//			$menuid=$_SESSION['menuid'];
//		}
//		$menuid=28;
//		$ss_menuid=",".$menuid.",";
		
		//获取编辑组 用户id
		$bianjiids=D("admin")->where("groupid<=3 and state=1")->select();
		foreach($bianjiids as $k=>$v){
			$bianjiid_str.=$v['id'].",";
		}
		$bianjiid_str=rtrim($bianjiid_str,",");
		//获取作家组 用户id
		$zuojiaids=D("admin")->where("groupid=5 and state=1")->select();
		foreach($zuojiaids as $k=>$v){
			$zuojiaid_str.=$v['id'].",";
		}
		$zuojiaid_str=rtrim($zuojiaid_str,",");
		//获取24小时前时间
		$yestday=time()-3600*24;
		//Must Read 编辑推荐
		$must_article=M('article')->where("state=1 and uid IN (".$bianjiid_str.") ")->order('stime desc,id desc')->limit(8)->select();
			$must_article=$this->get_lang_attr($must_article,"name");
		$this->assign('must_article', $must_article);

		//最多浏览
		$hot_article=M('article')->where("state=1 and uid NOT IN (".$zuojiaid_str.") or state=1 and uid IN (".$zuojiaid_str.") and stime<=".$yestday." ")->order('readss desc,id desc')->limit(8)->select();
			$hot_article=$this->get_lang_attr($hot_article,"name");
		$this->assign('hot_article', $hot_article);
		
		//最新报导
		$new_article=M('article')->where("state=1 and uid NOT IN (".$zuojiaid_str.") or state=1 and uid IN (".$zuojiaid_str.") and stime<=".$yestday." ")->order('stime desc,id desc')->limit(8)->select();
			$new_article=$this->get_lang_attr($new_article,"name");
		$this->assign('new_article', $new_article);
		
		//最多人收藏
		$fol_article=M('article')->where("state=1 and uid NOT IN (".$zuojiaid_str.") or state=1 and uid IN (".$zuojiaid_str.") and stime<=".$yestday." ")->order('follows desc,id desc')->limit(8)->select();
			$fol_article=$this->get_lang_attr($fol_article,"name");
		$this->assign('fol_article', $fol_article);
		
		if($toup['baoming']==0){
			$this->display();
		}else{
			$this->display('bisai');
		}
	}

	function getIP() /*获取客户端IP*/  
	{  
		if (@$_SERVER["HTTP_X_FORWARDED_FOR"])  
		$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];  
		else if (@$_SERVER["HTTP_CLIENT_IP"])  
		$ip = $_SERVER["HTTP_CLIENT_IP"];  
		else if (@$_SERVER["REMOTE_ADDR"])  
		$ip = $_SERVER["REMOTE_ADDR"];  
		else if (@getenv("HTTP_X_FORWARDED_FOR"))  
		$ip = getenv("HTTP_X_FORWARDED_FOR");  
		else if (@getenv("HTTP_CLIENT_IP"))  
		$ip = getenv("HTTP_CLIENT_IP");  
		else if (@getenv("REMOTE_ADDR"))  
		$ip = getenv("REMOTE_ADDR");  
		else  
		$ip = "Unknown";  
		return $ip;  
	}    
	  
	function dotoup(){
		$tid=$_POST['tid'];
		$sid=$_POST['sid'];
		$xid=$_POST['xid'];

		if(empty($_SESSION['uid'])){
			$ip=$this->getIP();
			
			$ip_cunzai=M('admin')->where("ip=".$ip."")->find();
			if($ip_cunzai){
				$_SESSION['uid']=$ip_cunzai['id'];
			}else{
				if($ip!="Unknown"){
					$ipattr=explode('.',$ip);
					$youke['nicheng']="游客".$ipattr[0]."***".$ipattr[3];
				}else{
					$youke['nicheng']="游客_未知";
				}
				$youke['ip']="".$ip."";
				$youke['ctime']=time();
				$youke['groupid']=7;
				$youke['state']=1;
				$youke_add=M('admin')->add($youke);
				$_SESSION['uid']=$youke_add;
			}

		}
		$uid=$_SESSION['uid'];
		
		$toup['uid']=$uid;
		$toup['tid']=$tid;
		$toup['sid']=$sid;
		$toup['xid']=$xid;
		$toup['ctime']=time();
		$toup['jishu']=1;
		$toup['state']=1;
		$ip=$this->getIP();
		$toup['name']=$ip;
		
		$today=date("Y-m-d",time());
		$today_shi=strtotime($today);
		$tomorrow=date("Y-m-d",time()+60*60*24);
		$tomorrow_shi=$today_shi+60*60*24;
		
		$toupjiu=M('tou_jieguo')->where("tid=".$tid." and uid=".$uid." and state=1 and ctime>=".$today_shi." and ctime<".$tomorrow_shi."")->count();
		if($toupjiu<10){
			$toupto=M('tou_jieguo')->add($toup);
			if($toupto){
				$cc=2;
			}else{
				$cc=0;
			}
		}else{
			$cc=3;
		}
		echo $toupjiu;
		exit;
	}  
	  
	function tous_info(){
		if( empty($_SESSION['uid']) ){
			$this->assign('jumpUrl',U('User/login'));
			$this->error("请登录！");
		}
		$tid=$_GET['tid'];
		$uid=$_SESSION['uid'];
		$admin_info=M('admin_info')->where("uid=".$uid."")->find();
		$this->assign('admin_info', $admin_info);
		
		$admin=M('admin')->find($uid);
		$this->assign('admin', $admin);
		
		if(empty($_GET['sid'])){
			$tou_shi_old=M('tou_shi')->where("tid=".$tid." and uid=".$uid."")->find();
			if(!empty($tou_shi_old)){
				$sid=$tou_shi_old['id'];
				$this->assign('jumpUrl',U('Toup/tous_info',array('tid'=>$tid)));
				$this->assign('tou_shi', $tou_shi_old);
			}else{
				$tou_shi_new['tid']=$tid;
				$tou_shi_new['uid']=$uid;
				//$tou_shi_new['name']=$admin['nicheng'];
				$tou_shi_new['ctime']=time();
				$tou_shi_new['state']=1;
				$tou_shi_add=M('tou_shi')->add($tou_shi_new);
				$sid=$tou_shi_add;
				$tou_shi_new['id']=$sid;
				$this->assign('tou_shi', $tou_shi_new);
			}
		}else{
			$sid=$_GET['sid'];
			$tou_shi=M('tou_shi')->where("state=1")->find($_GET['sid']);
			if( $_SESSION['uid']!=$tou_shi['uid']){
				$this->assign('jumpUrl',U('Toup/toup',array('tid'=>$tid)));
				$this->error("只能查看自己的信息！");
			}
			$this->assign('tou_shi', $tou_shi);
		}
		$tou_shi_info = M('tou_shi_info')->where("uid=".$uid." and tid=".$tid." and sid=".$sid."")->find();
		$this->assign('tou_shi_info', $tou_shi_info);
		
		$toup=M('tou')->where("state=1")->find($_GET['tid']);
		$this->assign('toup', $toup);
		
//		if(empty($_SESSION['menuid'])){
//			$all_menu = M('pz_menu')->where("mid=0 and state=1")->order("id asc")->select();
//			$all_menu_num = M('pz_menu')->where("mid=0 and state=1")->order("id asc")->count();
//			$rand_num=rand(0,$all_menu_num-1);
//			$menuid=$all_menu[$rand_num]['id'];
//		}else{
//			$menuid=$_SESSION['menuid'];
//		}
//		$menuid=28;
//		$ss_menuid=",".$menuid.",";
		
		//获取编辑组 用户id
		$bianjiids=D("admin")->where("groupid<=3 and state=1")->select();
		foreach($bianjiids as $k=>$v){
			$bianjiid_str.=$v['id'].",";
		}
		$bianjiid_str=rtrim($bianjiid_str,",");
		//获取作家组 用户id
		$zuojiaids=D("admin")->where("groupid=5 and state=1")->select();
		foreach($zuojiaids as $k=>$v){
			$zuojiaid_str.=$v['id'].",";
		}
		$zuojiaid_str=rtrim($zuojiaid_str,",");
		//获取24小时前时间
		$yestday=time()-3600*24;
		//Must Read 编辑推荐
		$must_article=M('article')->where("state=1 and uid IN (".$bianjiid_str.") ")->order('stime desc,id desc')->limit(8)->select();
			$must_article=$this->get_lang_attr($must_article,"name");
		$this->assign('must_article', $must_article);

		//最多浏览
		$hot_article=M('article')->where("state=1 and uid NOT IN (".$zuojiaid_str.") or state=1 and uid IN (".$zuojiaid_str.") and stime<=".$yestday." ")->order('readss desc,id desc')->limit(8)->select();
			$hot_article=$this->get_lang_attr($hot_article,"name");
		$this->assign('hot_article', $hot_article);
		
		//最新报导
		$new_article=M('article')->where("state=1 and uid NOT IN (".$zuojiaid_str.") or state=1 and uid IN (".$zuojiaid_str.") and stime<=".$yestday." ")->order('stime desc,id desc')->limit(8)->select();
			$new_article=$this->get_lang_attr($new_article,"name");
		$this->assign('new_article', $new_article);
		
		//最多人收藏
		$fol_article=M('article')->where("state=1 and uid NOT IN (".$zuojiaid_str.") or state=1 and uid IN (".$zuojiaid_str.") and stime<=".$yestday." ")->order('follows desc,id desc')->limit(8)->select();
			$fol_article=$this->get_lang_attr($fol_article,"name");
		$this->assign('fol_article', $fol_article);

		$this->display();
	}
	function dotous_info(){
    	if (!M()->autoCheckToken($_POST)){
    		$this->error('禁止外部提交！');
    	}else {
    		$uid=$_SESSION['uid'];
			$tid=t(h($_POST['tid']));
			$sid=t(h($_POST['sid']));
			
			$admin_s = M('admin')->find($uid);
    		$admin_s['adm_name'] = t(h($_POST['adm_name']));
			$admin_s_save=M('admin')->save($admin_s);
			
			$admin_info['uid'] = $uid;
			$admin_info['year'] = $_POST['year'];
			$admin_info['month'] = $_POST['month'];
			$admin_info['day'] = $_POST['day'];
			$admin_info['home'] = t(h($_POST['home']));
			$admin_info['area'] = t(h($_POST['area']));
			$admin_info['qq'] = $_POST['qq'];
			$admin_info['job'] = t(h($_POST['job']));
			$admin_info['education'] = t(h($_POST['education']));
			
			$admin_info_jiu = M('admin_info')->where("uid=".$uid."")->find();
			//判断是否存在旧资料 开始
			if($admin_info_jiu){
				$admin_info['id'] = $admin_info_jiu['id'];
				$admin_info_save=M('admin_info')->save($admin_info);
			}else {
				$admin_info['state']=1;
				$admin_info['ctime']=time();
				$admin_info_add=M('admin_info')->add($admin_info);
			}
			//判断是否存在旧资料 结束
			//保存封面照
			$tou_shi_jiu = M('tou_shi')->where("uid=".$uid." and tid=".$tid." and id=".$sid."")->find();
			$tou_shi_jiu['img'] = t(h($_POST['fengmian']));
			$tou_shi_jiu['name'] = t(h($_POST['name']));
			$tou_shi_save=M('tou_shi')->save($tou_shi_jiu);
			
			//保存报名人信息
			$tou_shi_info['uid'] = $uid;
			$tou_shi_info['tid'] = $tid;
			$tou_shi_info['sid'] = $sid;
			$tou_shi_info['name'] = t(h($_POST['name']));
			$tou_shi_info['shenfenid'] = t(h($_POST['shenfenid']));
			$tou_shi_info['minzu'] = t(h($_POST['minzu']));
			$tou_shi_info['chushengdi'] = t(h($_POST['chushengdi']));
			$tou_shi_info['shengao'] = t(h($_POST['shengao']));
			$tou_shi_info['tizhong'] = t(h($_POST['tizhong']));
			$tou_shi_info['xuexing'] = t(h($_POST['xuexing']));
			$tou_shi_info['xingzuo'] = t(h($_POST['xingzuo']));
			$tou_shi_info['weibo'] = t(h($_POST['weibo']));
			$tou_shi_info['zhuanye'] = t(h($_POST['zhuanye']));
			$tou_shi_info['danwei'] = t(h($_POST['danwei']));
			$tou_shi_info['sanwei'] = t(h($_POST['sanwei']));
			$tou_shi_info['jingyan'] = t(h($_POST['jingyan']));
			$tou_shi_info['bisai'] = t(h($_POST['bisai']));
			$tou_shi_info['fengmian'] = t(h($_POST['fengmian']));
			$tou_shi_info['sfz_zheng'] = t(h($_POST['sfz_zheng']));
			$tou_shi_info['sfz_fan'] = t(h($_POST['sfz_fan']));
			$tou_shi_info['lian1'] = t(h($_POST['lian1']));
			$tou_shi_info['lian2'] = t(h($_POST['lian2']));
			$tou_shi_info['suyan1'] = t(h($_POST['suyan1']));
			$tou_shi_info['suyan2'] = t(h($_POST['suyan2']));
			$tou_shi_info['quanshen1'] = t(h($_POST['quanshen1']));
			$tou_shi_info['quanshen2'] = t(h($_POST['quanshen2']));
			$tou_shi_info['xiezhen1'] = t(h($_POST['xiezhen1']));
			$tou_shi_info['xiezhen2'] = t(h($_POST['xiezhen2']));
			
			$tou_shi_info_jiu = M('tou_shi_info')->where("uid=".$uid." and tid=".$tid." and sid=".$sid."")->find();
			//判断是否存在旧资料 开始
			if($tou_shi_info_jiu){
				$tou_shi_info['id'] = $tou_shi_info_jiu['id'];
				$tou_shi_info_save=M('tou_shi_info')->save($tou_shi_info);
			}else {
				$tou_shi_info['state']=1;
				$tou_shi_info['ctime']=time();
				$tou_shi_info_add=M('tou_shi_info')->add($tou_shi_info);
			}
			if($tou_shi_info_save || $admin_s_save || $admin_info_save || $admin_info_add || $tou_shi_save || $tou_shi_info_add){
				$this->assign('jumpUrl', "./index.php?a=tous_info&m=toup&tid=".$tid."");
				$this->success("保存成功！");
			}else{
				$this->error("保存失败！");
			}
			//判断是否存在旧资料 结束
    	}
	}
	 
} 